var express = require('express');
var router = express.Router();
const moment = require('moment')

//门店地址列表
router.get('/storeAddress/list', function (req, res, next) {
    res.render('admin_tmpl/store_address/list.html', {});
});

//门店地址添加
router.get('/storeAddress/add', function (req, res, next) {
    res.render('admin_tmpl/store_address/add.html', {});
});

//门店地址修改
router.get('/storeAddress/edit', async (req, res, next) => {
    var id = req.query.id;
    var selectSQL = 'select * from t_store_address where delete_flag = 0 and id="' + id + '"'
    await common.prototype.operation(selectSQL).then(rest => {
        var storeAddress = rest[0];
        res.render('admin_tmpl/store_address/edit.html', storeAddress);
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })

});


/*
 * 门店地址列表
 * @return
 *
 */
router.post('/storeAddress/getList', async (req, res, next) => {
    var condition = '';
    var SQL = '';
    var conditionSQL = '';
    var allData = '';
    var begin = 0;
    var data = req.body;
    var pageSize = data.pageSize;
    var pageNumber = data.pageNumber;
    var likeName = req.body.search_like_name;
    if (likeName) {
        condition = condition + 'and position("' + likeName + '" IN name)'
    }
    SQL = 'select * from t_store_address where delete_flag = 0 ' + condition + ''
    await common.prototype.operation(SQL).then(async rest => {
        if (pageSize) {
            begin = (pageNumber - 1) * pageSize;
            condition = condition + 'order by id desc limit ' + begin + ',' + pageSize + ''
        }
        allData = rest;
        conditionSQL = 'select * from t_store_address where delete_flag = 0 ' + condition + ''
        await common.prototype.operation(conditionSQL).then(async result => {
            res.send({
                code: 0, msg: '操作成功',
                count: allData.length, data: result
            })
        })
    }, (error) => {
        console.log(error)
        res.send({
            code: 404, msg: '操作失败',
            data: error
        })
    }).catch((err) => {
        console.log(err)
    })
});


/*
 * 添加门店地址
 * @return
 *
 */
router.post('/storeAddress/addStoreAddress', async (req, res, next) => {
    var data = req.body;
    var date = moment().format('YYYY-MM-DD HH:mm:ss');
    var SQL = 'INSERT INTO `t_store_address`(address,name,longitude,latitude,create_date,modify_date,delete_flag) VALUES("' + data.address + '","' + data.name + '","' + data.longitude + '","' + data.latitude + '","' + date + '","' + date + '",0) '
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            type: 'success', content: '添加成功'
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '添加失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 删除门店地址
 * @return
 *
 */
router.post('/storeAddress/delete', async (req, res, next) => {
    var id = req.body.id;
    if (id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'update t_store_address set delete_flag=1, modify_date="' + date + '" where id="' + id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '删除成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '删除失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});

/*
 * 修改门店地址
 * @return
 *
 */
router.post('/storeAddress/update', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'update t_store_address set address="' + data.address + '",name="' + data.name + '",longitude="' + data.longitude + '",latitude="' + data.latitude + '", modify_date="' + date + '" where id="' + data.id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '添加成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '添加失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});


module.exports = router;